<extend name="./public/frame.html"/>
<block name="content">
    <style>
        .layui-form-label{width: 12%;}
        .layui-input-block{margin-left: 12%;}
    </style>
    <form class="layui-form layui-form-pane1" id="testForm" method="post" enctype="multipart/form-data">
        <table class="layui-table">
            <tr>
                <td style="text-align: center;width: 10%">评分</td>
                <td>
                    <div class="layui-input-inline" style="width: 250px;">
                        <select id="star_num" name="star_num" class="layui-input" lay-filter="is_display" style="width: 500px;">
                            <?php foreach($sorceList as $key=>$value):?>
                            <option value="<?php echo $key;?>" <?php if($key == $info['rank']):?>selected<?php endif;?>><?php echo $value;?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="text-align: center;width: 10%">评价内容</td>
                <td><input type="text" name="title" id="title" lay-verify="required" placeholder="评价内容" value="<?php echo $info['title'];?>" class="layui-input" style="width: 500px;" onkeyup="strleng(this)"></td>
            </tr>
            <tr>
                <td style="text-align: center;width: 10%">评价标签</td>
                <td>
                    <table class="layui-table biaoqian">
                        <?php if(empty($list)):?>
                        <tr>
                            <td>
                                <input type="text" name="label[]" placeholder="评价标签" class="layui-input" style="width: 200px;float:left;" id="label_1" maxlength="20" onkeyup="strleng(this)">
                                <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="look" style="width: 30px;margin-left: 10px;margin-top: 8px;" onclick="addBiao()">+</a>
                            </td>
                        </tr>
                        <?php else:?>
                        <?php foreach($list as $key=>$value):?>
                        <tr>
                            <td>
                                <input type="text" name="label[]" placeholder="评价标签" value="<?php echo $value['label'];?>" class="layui-input" style="width: 200px;float:left;" id="label_<?php echo $key+1?>" maxlength="20" onkeyup="strleng(this)">
                                <input type="hidden" value="<?php echo $value['id'];?>" name="label_id[]">
                                <?php if($key == 0):?>
                                <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="look" style="width: 30px;margin-left: 10px;margin-top: 8px;" onclick="addBiao()">+</a>
                                <?php else:?>
                                <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="look" style="width: 30px;margin-top: 8px;margin-left: 10px;float:left;" onclick="cutBiao(this)">-</a>
                                <?php endif;?>
                            </td>
                        </tr>
                        <?php endforeach;?>
                        <?php endif;?>
                    </table>
                </td>
            </tr>
            <tr>
                <td colspan="3" style="padding-left: 100px;">
                    <input type="hidden" value="<?php echo $info['id'];?>" name="id">
                    <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </td>
            </tr>
        </table>
    </form>
    <script>
        layui.use(['form'], function () {
            var form = layui.form;
            form.on('submit(*)', function(data){
                var title = $("#title").val();
                if(title == ''){
                    layer.msg('评价名称不能为空',{icon: 5});return false
                }
                $.ajax({
                    url:'{:url("/admin/CommentDict/edit")}',
                    async:true,
                    dataType:'json',
                    type:'POST',
                    data: $(data.form).serialize(),
                    success:function(res){
                        if(res.code==1){
                            layer.alert(res.msg, {icon: 6},function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload()
                            });
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                return false;
            });
        });
        function addBiao() {
            var html = '<tr>\n' +
                '            <td>\n' +
                '                 <input type="text" name="label[]" placeholder="评价标签" class="layui-input" style="width: 200px;float:left;" maxlength="20">\n' +
                '                 <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="look" style="width: 30px;margin-top: 8px;margin-left: 10px;float:left;" onclick="cutBiao(this)">-</a>\n' +
                '            </td>\n' +
                '       </tr>';
            $(".biaoqian").append(html);
        }
        function cutBiao($this) {
            $($this).parent().parent().remove();
        }
        //验证只能输入不能超过16个字符
        function strleng(object) {
            var value   = object.value;
            var id      = object.id;
            var cArr = value.match(/[^\x00-\xff]/ig);
            var num = value.length + (cArr == null ? 0 : cArr.length);
            if(num > 16){
                layer.msg('请输入不要超过16个字符',{icon: 5});
                $("#"+id).val('');
                return false;
            }
        }
    </script>
</block>
